package peer.rdp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.util.ArrayList;

class PacketProcessor implements Runnable{
	Route route;
	Thread mainThread;
	ArrayList<DatagramPacket> packetBuffer;
	Object lockOb;
	public static long i5=0;
	public static long i6=0;
	public static long i7=0;
	PacketProcessor(Route route){
		lockOb=new Object();
		packetBuffer=new  ArrayList<DatagramPacket>();
		this.route=route;
		mainThread=new Thread(this);
		mainThread.start();
	}
	
	public void run(){
		while(true){
			byte[] b=new byte[1024];
			DatagramPacket dp=new DatagramPacket(b,b.length);
			try {
				route.ds.receive(dp);
				packetBuffer.add(dp);
			} catch (IOException e) {
				e.printStackTrace();
				continue;
			}
			synchronized(lockOb){
				if(packetBuffer.size()!=0){
					lockOb.notifyAll();
				}
			}
		}
	}
	
	synchronized DatagramPacket getBufferPacket(){
		if(packetBuffer.size()==0){
			synchronized(lockOb){
				try {
					lockOb.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		DatagramPacket dp=packetBuffer.get(0);
		packetBuffer.remove(0);
		return dp;
	}
	
}
